package com.hxd.otc.module.wallet.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.module.wallet.adapter.CoinRecordAdapter;
import com.hxd.otc.module.wallet.bean.WalletCoinRecordBean;
import com.hxd.otc.module.wallet.contract.WalletCoinRecordContract;
import com.hxd.otc.module.wallet.presenter.CoinRecordPresenter;
import com.hxd.otc.widget.RecyclerListDivider;
import com.hxd.otc.widget.TitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class CoinRecordActivity extends BaseActivity<CoinRecordPresenter> implements WalletCoinRecordContract.View {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.rv_coin_record)
    RecyclerView rvCoinRecord;
    @BindView(R.id.smart_rf_coin_record)
    SmartRefreshLayout smartRfCoinRecord;
    View errorView;

    View notDataView;

    TextView tvRefresh;

    private CoinRecordAdapter mCoinRecordAdapter;
    private List<WalletCoinRecordBean.WalletCoinRecordData> datas;

    //是下拉刷新
    public static final int REFRESH = 0;
    //是上拉加载
    public static final int LOADMORE = 1;

    private long pageNo = 1;
    private long pageSize = 15;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wallet_coin_record;
    }

    @Override
    protected void initView() {
        titleBar.getTitle().setText(R.string.miv_pay_list);
        datas = new ArrayList<>();
        mCoinRecordAdapter = new CoinRecordAdapter(datas);
        rvCoinRecord.setAdapter(mCoinRecordAdapter);
        rvCoinRecord.setLayoutManager(new LinearLayoutManager(this));
        rvCoinRecord.addItemDecoration(new RecyclerListDivider(getResources().getColor(R.color.color_Line)));

        notDataView = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) rvCoinRecord.getParent(), false);
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) rvCoinRecord.getParent(), false);
        tvRefresh = errorView.findViewById(R.id.tv_refresh);
        tvRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                smartRfCoinRecord.autoRefresh(0, 10, 1);
            }
        });
    }

    @Override
    protected void initData() {
        smartRfCoinRecord.autoRefresh(0, 10, 1);
    }

    @Override
    protected void initEvent() {
        smartRfCoinRecord.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageNo = 1;
                mPresenter.getWalletCoinRecord(REFRESH, pageNo, pageSize);
            }
        });
        smartRfCoinRecord.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                pageNo += 1;
                mPresenter.getWalletCoinRecord(LOADMORE, pageNo, pageSize);
            }
        });
        mCoinRecordAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (datas != null && datas.size() > 0) {
                    Intent intent = new Intent(CoinRecordActivity.this, CoinRecordDetailActivity.class);
                    intent.putExtra("info", datas.get(position));
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected CoinRecordPresenter createPresenter() {
        return new CoinRecordPresenter(this);
    }


    @Override
    public void refreshData(List<WalletCoinRecordBean.WalletCoinRecordData> data) {
        if (data != null) {
            if (datas != null) {
                datas.clear();
                datas.addAll(data);
            }
            if (mCoinRecordAdapter != null) mCoinRecordAdapter.notifyDataSetChanged();

            if (datas != null && datas.size() <= 0) {
                mCoinRecordAdapter.setEmptyView(notDataView);
            }
            //小于15个不允许上拉加载
            if (data == null || data.size() < pageSize) {
                setEnableLoadMore(false);
            } else {
                setEnableLoadMore(true);
            }
        }
    }

    @Override
    public void loadMoreData(List<WalletCoinRecordBean.WalletCoinRecordData> data) {
        if (data != null) {
            if (datas != null) {
                datas.addAll(data);
            }
            if (mCoinRecordAdapter != null) mCoinRecordAdapter.notifyDataSetChanged();
            //小于15个不允许上拉加载
            if (data == null || data.size() < pageSize) {
                setEnableLoadMore(false);
            } else {
                setEnableLoadMore(true);
            }
        }

    }

    @Override
    public void finishRefresh() {
        if (smartRfCoinRecord != null) {
            smartRfCoinRecord.finishRefresh();
        }
    }

    @Override
    public void finishLoadMore() {
        if (smartRfCoinRecord != null) {
            smartRfCoinRecord.finishLoadMore();
        }
    }

    @Override
    public void setEnableLoadMore(boolean b) {
        if (smartRfCoinRecord != null) {
            smartRfCoinRecord.setEnableLoadMore(b);
        }
    }

    @Override
    public void onError(int code) {
        super.onError(code);
        mCoinRecordAdapter.setEmptyView(errorView);
    }
}
